package cn._2dland.gae.httpclient.method;

import java.util.Iterator;
import java.util.Map.Entry;

public class PostMethod extends HttpMethod {
	public PostMethod() {
		super();
		addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
	public PostMethod(String url) {
		super(url);
		addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}

	public String getMethodName() {
		return "POST";
	}

	public String getURL() {
		return this.url;
	}

	public byte[] getData() {
		byte[] data = null;

		if(parameters != null) {
			Iterator<Entry<String, String>> ps = parameters.entrySet().iterator();
			StringBuffer paramBuf = new StringBuffer();
			while(ps.hasNext()) {
				Entry<String, String> p = ps.next();
				paramBuf.append(p.getKey()).append("=").append(p.getValue()).append("&");
			}
			if(paramBuf.charAt(paramBuf.length() - 1) == '&')
				paramBuf = paramBuf.deleteCharAt(paramBuf.length() - 1);

			String paramStr = paramBuf.toString();
			try { data = paramStr.getBytes(params.getClientCharset()); }
			catch (Exception e) { data = paramStr.getBytes(); }
		}
		return data;
	}
}
